﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace Maniac
{
    public partial class DayAggregateControl : UserControl
    {
        public DayAggregateControl()
        {
            InitializeComponent();
        }
        private DayAggregate ivdDayAggregate;
        public DayAggregate DayAggregate
        {
            get
            {
                return ivdDayAggregate;
            }
            set
            {
                ivdDayAggregate = value;
                this.Invalidate();
            }
        }

       

        private void DayAggregateControl_Paint(object sender, PaintEventArgs e)
        {
            Color shineColor = CalculateShineColor();

            Color c1 = ControlPaint.Light(shineColor);            
            Color c2 = ControlPaint.LightLight(shineColor);
			
            Rectangle tvrArea = new Rectangle(0,0,this.Width, this.Height);
            
            GradientHelper.GlossyRect(e.Graphics, tvrArea, c2, c1, shineColor, c1);
            DrawLogo(e.Graphics, tvrArea);
			//if (ivdDayAggregate.IsExDividendDate)
			//	DrawExDividend(e.Graphics, tvrArea);
			
            DrawTextInformation(e.Graphics, tvrArea);

            if (this.Focused)
            {
                Pen tvPen = new Pen(Color.White);
                tvPen.Width = 4.0f;
                e.Graphics.DrawRectangle(tvPen, e.ClipRectangle);
            }
            tvrArea.Height--;
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Gray)), tvrArea);
						
        }
		
        private void DrawLogo(Graphics g, Rectangle r)
        {
            if (ivdDayAggregate == null)
                return;
            Image tviLogoImage = (Image)Properties.Resources.ResourceManager.GetObject(ivdDayAggregate.Symbol);
            if (tviLogoImage != null)
            {
                Size ThumbnailSize = new Size(Math.Max(r.Width - 25, 1), Math.Max(r.Height / 2, 1));                

                float WidthPer, HeightPer;
                int NewWidth, NewHeight;
                float thumbnailAspectRatio = (float)ThumbnailSize.Width / ThumbnailSize.Height;
                float origImageAspectRatio = (float)tviLogoImage.Width / tviLogoImage.Height;


                if (thumbnailAspectRatio < origImageAspectRatio)
                {
                    NewWidth = ThumbnailSize.Width;
                    WidthPer = (float)ThumbnailSize.Width / tviLogoImage.Width;
                    NewHeight = Convert.ToInt32(tviLogoImage.Height * WidthPer);
                }
                else
                {
                    NewHeight = ThumbnailSize.Height;
                    HeightPer = (float)ThumbnailSize.Height / tviLogoImage.Height;
                    NewWidth = Convert.ToInt32(tviLogoImage.Width * HeightPer);
                }

                Bitmap origThumbnail = new Bitmap(NewWidth, NewHeight, tviLogoImage.PixelFormat);
                Graphics oGraphic = Graphics.FromImage(origThumbnail);
                oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

                Rectangle oRectangle = new Rectangle(0, 0, NewWidth, NewHeight);
                oGraphic.DrawImage(tviLogoImage, oRectangle);

                origThumbnail.MakeTransparent(Color.White);

                int tviX = r.Left + (r.Width - NewWidth) / 2;
                int tviY = r.Top + 10;// +(e.Bounds.Height - NewHeight) / 2;

                //ColorMatrix matrix = new ColorMatrix();

                ColorMatrix matrix = new ColorMatrix(new float[][]{   new float[]{0.5f,0.5f,0.5f,0,0},
                                  new float[]{0.5f,0.5f,0.5f,0,0},
                                  new float[]{0.5f,0.5f,0.5f,0,0},
                                  new float[]{0,0,0,1,0,0},
                                  new float[]{0,0,0,0,1,0},
                                  new float[]{0,0,0,0,0,1}});
                matrix.Matrix33 = 0.5f; //opacity 0 = completely transparent, 1 = completely opaque

                ImageAttributes attributes = new ImageAttributes();
                attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                g.DrawImage(origThumbnail, new Rectangle(tviX, tviY, origThumbnail.Width - 2, origThumbnail.Height - 2),
             0, 0,
             origThumbnail.Width, origThumbnail.Height,
             GraphicsUnit.Pixel,
             attributes);
            }
        }
        private void DrawTextInformation(Graphics g, Rectangle r)
        {
            if (ivdDayAggregate == null)
                return;
            Font tvfFont = new Font(FontFamily.GenericSansSerif, 14f, FontStyle.Bold, GraphicsUnit.Pixel);//(SystemFonts.CaptionFont, FontStyle.Bold);

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            g.DrawString(ivdDayAggregate.Symbol, tvfFont, Brushes.Black, new PointF(4, 4));
            g.DrawString(ivdDayAggregate.Symbol, tvfFont, Brushes.White, new PointF(3, 3));

            g.DrawString(ivdDayAggregate.RiseProbability.ToString(CultureInfo.CurrentCulture), tvfFont, Brushes.Black, new PointF(r.Right - 50, 4));
            g.DrawString(ivdDayAggregate.RiseProbability.ToString(CultureInfo.CurrentCulture), tvfFont, Brushes.White, new PointF(r.Right - 51, 3));

            //this should reflecy gain type setting.
            g.DrawString(ivdDayAggregate.AverageGainPercentPriorClose.ToString(CultureInfo.CurrentCulture) + "%", tvfFont, Brushes.Black, new PointF(r.Right - 50, r.Bottom - 17));
            g.DrawString(ivdDayAggregate.AverageGainPercentPriorClose.ToString(CultureInfo.CurrentCulture) + "%", tvfFont, Brushes.White, new PointF(r.Right - 51, r.Bottom - 18));

            string tvsLoss = "x";
            /*if (ivdDayAggregate.RiseProbability >= 50 && ivdDayAggregate.MostRecentActualGain > 0)
                tvsLoss = "$";
            if (ivdDayAggregate.RiseProbability <= 50 && ivdDayAggregate.MostRecentActualGain < 0)
                tvsLoss = "$";
                */
            g.DrawString(tvsLoss, tvfFont, Brushes.Black, new PointF(4, r.Bottom - 17));
            g.DrawString(tvsLoss, tvfFont, Brushes.White, new PointF(3, r.Bottom - 18));
            //g.DrawString((ivdDayAggregate.AverageGainPercentPriorClose - ivdDayAggregate.GainPercentStandardDeviation).ToString(), tvfFont, Brushes.Black, new PointF(r.Right - 45, r.Bottom - 17));
            //g.DrawString((ivdDayAggregate.AverageGainPercentPriorClose - ivdDayAggregate.GainPercentStandardDeviation).ToString(), tvfFont, Brushes.White, new PointF(r.Right - 46, r.Bottom - 18));
        }
/*
        public static void GradientRect(Graphics g, Rectangle bounds, Color a, Color b)
        {
            using (LinearGradientBrush br = new LinearGradientBrush(bounds, b, a, -90))
            {
                g.FillRectangle(br, bounds);
            }
        }

        public static void GlossyRect(Graphics g, Rectangle bounds, Color a, Color b, Color c, Color d)
        {
            g.FillRectangle(new SolidBrush(c), bounds);
            Rectangle top = new Rectangle(bounds.Left, bounds.Top, bounds.Width, bounds.Height / 2);
            Rectangle bot = Rectangle.FromLTRB(bounds.Left, top.Bottom, bounds.Right, bounds.Bottom);

            GradientRect(g, top, a, b);
            GradientRect(g, bot, c, d);
        }
  */      private Color CalculateShineColor()
        {            
            Color tvcReturn = Color.White;
            if (ivdDayAggregate == null)
                return tvcReturn;
            if (ivdDayAggregate.RiseProbability > Convert.ToDecimal(85))
                tvcReturn = Color.FromArgb(15,170, 29);
            else if (ivdDayAggregate.RiseProbability > Convert.ToDecimal(60))
                tvcReturn = Color.FromArgb(1, 42, 9);
            else if (ivdDayAggregate.RiseProbability >= 50)
                tvcReturn = Color.FromArgb(6, 8, 6);
            else if (ivdDayAggregate.RiseProbability < Convert.ToDecimal(15))
                tvcReturn = Color.FromArgb(206,0,20);
            else if (ivdDayAggregate.RiseProbability < Convert.ToDecimal(30))
                tvcReturn = Color.FromArgb(94, 2, 9);
            else if (ivdDayAggregate.RiseProbability < Convert.ToDecimal(50))
                tvcReturn = Color.FromArgb(6, 8, 6);
            return tvcReturn;
        }

        private void DayAggregateControl_Resize(object sender, EventArgs e)
        {
           // this.Invalidate();
			if(this.Height>this.MaximumSize.Height)
				this.Height = this.MaximumSize.Height;
        }

      
    }
}
